//
//  LoginViewController.m
//  RainbowFM
//
//  Created by chenli on 14/12/8.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "LoginViewController.h"
#import "PhoneViewController.h"
#import "LoginModel.h"

@interface LoginViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *phoneNumField;
@property (weak, nonatomic) IBOutlet UITextField *pwdField;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UIImageView *userImageView;
@property (weak, nonatomic) IBOutlet UIImageView *pwdImageView;

@end

@implementation LoginViewController


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pwdField.secureTextEntry = YES;
    // Do any additional setup after loading the view.
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField == self.phoneNumField) {
        self.userImageView.image = [UIImage imageNamed:@"login_user_icon_1"];
        self.phoneNumField.textColor = [UIColor redColor];
    }
    else if(textField == self.pwdField){
        self.pwdImageView.image = [UIImage imageNamed:@"login_pwd_icon_1"];
        self.pwdField.textColor = [UIColor redColor];
    }
}


-(void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField == self.phoneNumField) {
        self.userImageView.image = [UIImage imageNamed:@"login_user_icon"];
        self.phoneNumField.textColor = [UIColor lightGrayColor];
    }
    else if(textField == self.pwdField){
        self.pwdImageView.image = [UIImage imageNamed:@"login_pwd_icon"];
        self.pwdField.textColor = [UIColor lightGrayColor];
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)loginingAction:(id)sender {
    [SVProgressHUD show];
    self.loginButton.enabled = NO;
    [[LoginModel sharedModel] loginWithPhoneNum:_phoneNumField.text password:_pwdField.text result:^(BOOL success, NSString *msg) {
        self.loginButton.enabled = YES;
        if (success) {
            NSUserDefaults *userLogin = [[NSUserDefaults alloc] init];
            [userLogin setObject:_phoneNumField.text forKey:@"phone"];
            [userLogin setObject:_pwdField.text forKey:@"pwd"];
            [userLogin synchronize];
            [SVProgressHUD showSuccessWithStatus:msg];
            [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        }else{
            [SVProgressHUD showErrorWithStatus:msg];
        }
    }];
//    _Nav2Board(@"Main");
//    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"forgetPwd"]) {
        [[segue destinationViewController] setTitle:@"忘记密码"];
    }
    else{
        [[segue destinationViewController] setTitle:@"用户注册"];
    }
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

- (IBAction)backAction:(id)sender {
    UIViewController *ctrl = [self.navigationController popViewControllerAnimated:YES];
    if (ctrl == nil) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

@end
